package com.i5tong.dbviewerlibrary;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.i5tong.dbviewerlibrary.adapter.DbFileRecyclerViewAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class DBViewerActivity extends Activity implements DbFileRecyclerViewAdapter.OnDbFileChoose {

    private RecyclerView mRecyclerView;
    private List<String> datas;
    private DbFileRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dbviewer);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        initDataBaseConnection();
    }

    private void initDataBaseConnection() {
        datas = new ArrayList<>();
        File db_file_dir = new File(this.getFilesDir().getParent() + "/databases");
        if (db_file_dir.exists()) {
            File[] child_files = db_file_dir.listFiles();
            for (File file : child_files) {
                if (!file.isDirectory() && !file.getName().endsWith("-journal")) {
                    datas.add(file.getName());
                }
            }
        }
        adapter = new DbFileRecyclerViewAdapter(this, datas);
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    public void click(String filename) {
        Intent intent = new Intent(this, TableListAvtivity.class);
        intent.putExtra("db_name", filename);
        startActivity(intent);
    }

}
